(function (global, factory) {
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  typeof define === 'function' && define.amd ? define(['exports'], factory) :
  (global = global || self, factory(global['yly-form-custom-script'] = {}));
}(this, (function (exports) { 'use strict';

  function _defineProperty(e, r, t) {
    return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
      value: t,
      enumerable: !0,
      configurable: !0,
      writable: !0
    }) : e[r] = t, e;
  }
  function _toPrimitive(t, r) {
    if ("object" != typeof t || !t) return t;
    var e = t[Symbol.toPrimitive];
    if (void 0 !== e) {
      var i = e.call(t, r || "default");
      if ("object" != typeof i) return i;
      throw new TypeError("@@toPrimitive must return a primitive value.");
    }
    return ("string" === r ? String : Number)(t);
  }
  function _toPropertyKey(t) {
    var i = _toPrimitive(t, "string");
    return "symbol" == typeof i ? i : i + "";
  }

  /**
   * 函数校验器
   * @param {*} codeScriptStr 
   * @returns 
   */
  var validateLowCodeScript = codeScriptStr => {
    try {
      if (codeScriptStr && codeScriptStr.trim()) {
        // 校验是否有不允许的条件
        // if() {
        //   throw new Error(`代码中不允许的条件`)
        // }
        return {
          result: true
        };
      }
      throw new Error('脚本为空');
    } catch (err) {
      console.error(err);
      return {
        result: false,
        msg: err.message
      };
    }
  };

  // var babel = require('./babel-standalone/babel.min.js')
  class CustomScript {
    getInstance() {
      this.instance = this.instance || new CustomScript();
      return this.instance;
    }
    executeCodeScript(codeScriptStr, vm) {
      try {
        // 校验代码
        if (!validateLowCodeScript(codeScriptStr)) {
          throw new Error('代码校验失败');
        }
        // var output = babel.transform(
        //   ` var getCodeScript = () => {
        //     ${codeScriptStr}
        //     }
        //   `,
        //   { presets: ['es2015', 'stage-2'] }
        // )
        // 生成执行单元
        // if 入股需要放在函数里 需要执行函数
        var codeExecutorFunc = new Function('vm', `
          return (value, key) => { ${codeScriptStr}};
        `);
        return codeExecutorFunc.call(vm, vm);
      } catch (error) {
        throw error;
      } finally {
        // 处理已经生成的函数
      }
    }
  }
  _defineProperty(CustomScript, "instance", void 0);

  exports.CustomScript = CustomScript;

  Object.defineProperty(exports, '__esModule', { value: true });

})));
